package util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

public class FileTransferConfig
{
	// Client config
	public Properties prop;
	public String clienthost = "localhost";
	public String clientport = "17284";
		
	// Server config
	public String serverport = "17284";
	public String serverdir = System.getProperty("user.dir");
	
	public FileTransferConfig() throws IOException
	{
		prop = new Properties();
		try {
			prop.load(new FileInputStream("config.properties"));
			
			clienthost = prop.getProperty("client_host");
			clientport = prop.getProperty("client_port");
			serverport = prop.getProperty("server_port");
			serverdir = prop.getProperty("server_dir");
			
		} catch(FileNotFoundException e) {
			setConfig();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void setConfig() throws IOException
	{
		OutputStream out = new FileOutputStream("config.properties");
		try{
			prop.setProperty("client_host", clienthost);
			prop.setProperty("client_port", clientport);
			prop.setProperty("server_port", serverport);
			prop.setProperty("server_dir", serverdir);
			
			//save properties to project root folder
			prop.store(out, null);
		} catch(FileNotFoundException e) {
			e.printStackTrace();
		} catch(IOException e) {
			e.printStackTrace();
		} finally{
			out.close();
		}
	}
	
}